import 'package:flutter/material.dart';
import 'package:flutter_module/style/colors.dart';

///自定义Dialog
class CustomDialog extends StatefulWidget {
  //------------------不带图片的dialog------------------------
  final String? title; //弹窗标题
  final String? content; //弹窗内容
  final String? confirmContent; //按钮文本
  final Color? confirmTextColor; //确定按钮文本颜色

  final Color? cancelTextColor; //取消按钮文本颜色
  final String? cancelContent; //取消按钮文本
  final bool isCancel; //是否有取消按钮，默认为true true：有 false：没有
  final Color? confirmColor; //确定按钮颜色
  final Color? cancelColor; //取消按钮颜色
  final bool outsideDismiss; //点击弹窗外部，关闭弹窗，默认为true true：可以关闭 false：不可以关闭
  final Function? confirmCallback; //点击确定按钮回调
  final Function? dismissCallback; //弹窗关闭回调

  const CustomDialog({
    Key? key,
    this.title,
    this.content,
    this.confirmContent,
    this.confirmTextColor,
    this.cancelContent,
    this.cancelTextColor,
    this.isCancel = true,
    this.confirmColor,
    this.cancelColor,
    this.outsideDismiss = true,
    this.confirmCallback,
    this.dismissCallback,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CustomDialogState();
  }
}

class _CustomDialogState extends State<CustomDialog> {
  final double radius = 5.0;

  _confirmDialog() {
    if (widget.confirmCallback != null) {
      widget.confirmCallback!();
    }
    Navigator.of(context).pop();
  }

  _dismissDialog() {
    if (widget.dismissCallback != null) {
      widget.dismissCallback!();
    }
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final width = size.width;
    return WillPopScope(
        child: GestureDetector(
          onTap: () => {widget.outsideDismiss ? _dismissDialog() : null},
          child: Material(
            type: MaterialType.transparency,
            child: _getWidget(width),
          ),
        ),
        onWillPop: () async {
          return widget.outsideDismiss;
        });
  }

  _getWidget(double width) {
    return Material(
      type: MaterialType.transparency,
      child: _getCenter(width),
    );
  }

  _getCenter(double width) {
    return Center(
        child: Container(
      width: width - 100.0,
      alignment: Alignment.center,
      child: Container(
        child: _columnText(),
        decoration: BoxDecoration(
            color: Color(0xFFFFFFFF),
            borderRadius: BorderRadius.circular(radius)),
      ),
    ));
  }

  _columnText() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        SizedBox(height: widget.title == null ? 0 : 16.0),
        Text(widget.title ?? '',
            style: TextStyle(
                fontSize: 16.0,
                color: ThemeColors.text333,
                fontWeight: FontWeight.w500)),
        Container(
          alignment: Alignment.center,
          padding: EdgeInsets.only(
              left: 13,
              right: 13,
              top: widget.title == null ? 0 : 12,
              bottom: 13),
          constraints: BoxConstraints(
            minHeight: widget.title == null ? 60 : 20,
          ),
          child: Text(widget.content ?? '',
              style: TextStyle(
                  fontSize: 14.0,
                  color: widget.title == null
                      ? ThemeColors.text333
                      : ThemeColors.text888,
                  fontWeight: widget.title == null
                      ? FontWeight.w500
                      : FontWeight.normal)),
        ),
        SizedBox(height: 1.0, child: Container(color: Color(0xDBDBDBDB))),
        Container(
            height: 45,
            child: Row(
              children: <Widget>[
                Expanded(
                    child: widget.isCancel
                        ? Container(
                            decoration: BoxDecoration(
                                color: widget.cancelColor == null
                                    ? Color(0xFFFFFFFF)
                                    : widget.cancelColor,
                                borderRadius: BorderRadius.only(
                                    bottomLeft: Radius.circular(radius))),
                            child: TextButton(
                              child: Text(widget.cancelContent ?? '取消',
                                  style: TextStyle(
                                    fontSize: 16.0,
                                    color: widget.cancelColor == null
                                        ? (widget.cancelTextColor == null
                                            ? ThemeColors.text333
                                            : widget.cancelTextColor)
                                        : Color(0xFFFFFFFF),
                                  )),
                              onPressed: _dismissDialog,
                            ),
                          )
                        : Text(''),
                    flex: widget.isCancel ? 1 : 0),
                SizedBox(
                    width: widget.isCancel ? 1.0 : 0,
                    child: Container(color: Color(0xDBDBDBDB))),
                Expanded(
                    child: Container(
                      decoration: BoxDecoration(
                          color: widget.confirmColor == null
                              ? Color(0xFFFFFFFF)
                              : widget.confirmColor,
                          borderRadius: widget.isCancel
                              ? BorderRadius.only(
                                  bottomRight: Radius.circular(radius))
                              : BorderRadius.only(
                                  bottomLeft: Radius.circular(radius),
                                  bottomRight: Radius.circular(radius))),
                      child: TextButton(
                        onPressed: _confirmDialog,
                        child: Text(widget.confirmContent ?? '确定',
                            style: TextStyle(
                              fontSize: 16.0,
                              color: widget.confirmColor == null
                                  ? (widget.confirmTextColor == null
                                      ? ThemeColors.redEF5D5E
                                      : widget.confirmTextColor)
                                  : Color(0xFFFFFFFF),
                            )),
                      ),
                    ),
                    flex: 1),
              ],
            ))
      ],
    );
  }
}
